package cn.itcast.controller;

import cn.itcast.common.R;
import cn.itcast.entity.Category;
import cn.itcast.entity.Page;
import cn.itcast.service.ICategoryService;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static cn.itcast.common.R.success;

@RestController
@RequestMapping("category")
public class CategoryController extends BaseController {
    @Autowired
    ICategoryService categoryService;

    /**
     * 展示数据
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page<Category>> page(Integer page, Integer pageSize){
        Page<Category> pageInfo = categoryService.findByPage(page, pageSize);

        return success(pageInfo);

    }

    /**
     * 添加数据
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
      categoryService.save(category);
      return success("保存成功");
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping()
    public R<String> delete(Long id){
           categoryService.delete(id);
           return success("删除成功");
    }
    /**
     * 修改数据
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
     categoryService.update(category);
     return success("修改数据成功");
    }

    @GetMapping("exportExcel")
    @SneakyThrows
    public void exportExcel(){
        //设置响应类型
       response.setContentType("application/vnd.ms-excel");
       //以下载附件的方式打开
       response.setHeader("content-disposition","attachment;filename=category.xlsx");
        //读取模板的输入流
        InputStream inputStream = getClass().getResourceAsStream("/excel/category_template.xlsx");
        //使用模板输入流构建一个工作簿
        Workbook workbook = new XSSFWorkbook(inputStream);
        //得到第0个工作薄
        Sheet sheet = workbook.getSheetAt(0);


        //读取第二行,提取到第二行的样式，并且存储到集合中
        Row row = sheet.getRow(1);
        ArrayList<CellStyle> cellStyleList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            cellStyleList.add(cellStyle);
        }

        //调用业务层的数据，并把数据写入到excel

        List<Category> categories = categoryService.findAll();
        for (int i = 0; i < categories.size(); i++) {
            //从第二行开始写
             row = sheet.createRow(i + 1);
             //获取元素
            Category category = categories.get(i);
            //创建第一列的单元格
            Cell cell = row.createCell(0);
            //设置第一个单元格的类别类型的值和单元格式
            cell.setCellValue(category.getType());
            cell.setCellStyle(cellStyleList.get(0));

            //创建第二列的单元格
           cell = row.createCell(1);
            //设置值和单元格式
            cell.setCellValue(category.getName());
            cell.setCellStyle(cellStyleList.get(1));

            //设置第三列的单元格
            cell =row.createCell(2);
            cell.setCellValue(category.getSort());
            cell.setCellStyle(cellStyleList.get(2));

            //设置第四列的单元格
            cell=row.createCell(3);
            cell.setCellValue(category.getCreateTime().toString());
            cell.setCellStyle(cellStyleList.get(3));

            //设置第五列的单元格
            cell=row.createCell(4);
            cell.setCellValue(category.getUpdateTime().toString());
            cell.setCellStyle(cellStyleList.get(4));
        }

        workbook.write(response.getOutputStream());
        workbook.close();

    }


    @GetMapping("list")
    public R<List<Category>> list(Integer type){

      return R.success(categoryService.list(type));
    }
}
